﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using System.Data;
public partial class administrator_uc_admin_e_menu : WebPageSecurity
{
    protected void Page_Load(object sender, EventArgs e)
    {        
        if (!IsPostBack)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["itemid"]))
            {
                LoadDataToDropDownList(DropdownParentID, 0);
                LoadDataInfo(int.Parse(Request.QueryString["itemid"].ToString().Trim()));                
            }
        }
    }
    private void LoadDataToDropDownList(DropDownList cboName, int MenuID)
    {
        ListItem list = new ListItem();
        cboName.Items.Clear();
        cboName.Items.Add(list);
        list.Selected = true;
        list.Value = "0";
        list.Text = "---Chọn menu cha---";
        DataTable dtb = new CmsBLL().Menu_GetTree(MenuID);
        if (dtb.Rows.Count > 0)
        {
            new CommonBLL().LoadDropDonwList(cboName, dtb, "_Title", "MenuID");
        }
    }
    private void LoadDataInfo(int ItemID)
    {
        DataTable dt = new BLL.CmsBLL().Menu_GetTree(ItemID);
        if (dt.Rows.Count > 0)
        {
            hdItemID.Value = dt.Rows[0]["MenuID"] + "";
            DropdownParentID.SelectedValue = dt.Rows[0]["ParentID"] + "";
            txtTitle.Text = dt.Rows[0]["Title"] + "";
            txtDescription.Text = dt.Rows[0]["Description"] + "";
            txtUrl.Text = dt.Rows[0]["Url"] + "";
            DropDownListLocation.SelectedValue = dt.Rows[0]["Location"] + "";
            rdTarget.SelectedValue = dt.Rows[0]["Target"] + "";
            txtSortIndex.Text = dt.Rows[0]["SortIndex"] + "";
            rdActive.SelectedValue = dt.Rows[0]["Active"] + "";
        }
        else
            lblMessage.Text = "Load data failt!";
    }       
   
    protected void lbtEditItem_Click(object sender, EventArgs e)
    {
        try
        {
            new CmsBLL().Menu_UpDateItem(int.Parse(hdItemID.Value), int.Parse("0" + DropdownParentID.SelectedValue), txtTitle.Text.Trim(), txtDescription.Text.Trim(), txtUrl.Text.Trim(), int.Parse("0" + DropDownListLocation.SelectedValue), int.Parse("0" + rdTarget.SelectedValue), int.Parse("0" + txtSortIndex.Text.Trim()), int.Parse("0" + rdActive.SelectedValue));
            lblMessage.Text = "Cập nhật thành công!";           
        }
        catch (Exception ex)
        {
            lblMessage.Text = "ERROR! " + ex.Message.ToString();
        }
    }
}